package com.solution.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

import com.solution.exception.DAOException;
import com.solution.model.PO;
import com.solution.model.SchdJobAssociations;
import com.solution.util.SearchVO;

/**
 * Exposes the CRUD functionalities for SchdJobAssociations table.
 * 
 * 
 */
public class SchdJobAssociationsDAOImpl extends DAOImpl<SchdJobAssociations>
		implements SchdJobAssociationsDAO {

	/**
	 * Logger static object.
	 */
	private Logger log = Logger.getLogger(SchdJobAssociationsDAOImpl.class
			.getName());

	@Override
	protected Class<? extends PO> getPOClass() {
		return SchdJobAssociations.class;
	}

	public String getJobAssociationParameter(String jobName, String jobGroup,
			String triggerName, String triggerGroup) throws DAOException {
		try {
			log.debug("find job association parameter for jobName:" + jobName
					+ " jobGroup:" + jobGroup + " triggerName:" + triggerName
					+ " triggerGroup:" + triggerGroup);
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("schdJobDetails.jobName", jobName);
			searchVO.addEqualsCondition("schdJobDetails.jobGroup", jobGroup);
			searchVO.addEqualsCondition("schdCronTriggers.triggerName",
					triggerName);
			searchVO.addEqualsCondition("schdCronTriggers.triggerGroup",
					triggerGroup);
			searchVO.addEqualsCondition("isActive", true);
			searchVO.addOrderDescending("id");
			SchdJobAssociations schdJobAssociations = (SchdJobAssociations) search(
					searchVO).get(0);
			return schdJobAssociations.getParameters();
		} catch (HibernateException he) {
			throw new DAOException(he);
		}
	}

	public String getJobAssociationParameter(String jobName, String jobGroup)
			throws DAOException {
		try {
			log.debug("find job association parameter for jobName:" + jobName
					+ " jobGroup:" + jobGroup);
			SearchVO searchVO = new SearchVO();
			searchVO.addEqualsCondition("schdJobDetails.jobName", jobName);
			searchVO.addEqualsCondition("schdJobDetails.jobGroup", jobGroup);
			searchVO.addEqualsCondition("schdCronTriggers.id", new Long(0));
			searchVO.addEqualsCondition("isActive", false);
			searchVO.addOrderDescending("id");

			SchdJobAssociations schdJobAssociations = (SchdJobAssociations) search(
					searchVO).get(0);
			return schdJobAssociations.getParameters();
		} catch (HibernateException he) {
			throw new DAOException(he);
		}
	}

}
